package com.zmj.sy.mom.srv.aps.config;

import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseResVo;
import com.zmj.sy.mom.srv.aps.utils.SyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionHandle {

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.OK)
    public BaseResVo exceptionHandle(Exception e) {
        log.error(e.getMessage(), e);
        return BaseResVo.fail(e.getMessage());
    }

    @ExceptionHandler(value = SyException.class)
    @ResponseStatus(HttpStatus.OK)
    public BaseResVo syException(SyException e) {
        log.error(e.getMessage(), e);
        return BaseResVo.fail(e.getMessage());
    }

    @ExceptionHandler(value = BindException.class)
    @ResponseStatus(HttpStatus.OK)
    public BaseResVo bindException(BindException e) {
        return BaseResVo.fail(e.getAllErrors().get(0).getDefaultMessage());
    }

}
